//
//  UIImageView+WebImage.m
//  YppSpecificationImage
//
//  Created by 胡润辰 on 2019/1/31.
//

#import "UIImageView+WebImage.h"
#import "SDVersion.h"

@implementation UIImageView (WebImage)

- (void)webImageWithURL:(NSURL *)url
              imageSize:(CGSize)size
            placeHolder:(UIImage *)placeholder {
    [self webImageWithURL:url imageSize:size placeHolder:placeholder options:YppWebImageOptionShowNetworkActivity];
}

- (void)webImageWithURL:(NSURL *)url
              imageSize:(CGSize)size
            placeHolder:(UIImage *)placeholder
               priority:(NSOperationQueuePriority)priority{
    [self webImageWithURL:url imageSize:size placeholder:placeholder priority:priority options:YppWebImageOptionShowNetworkActivity progress:nil transform:nil completion:nil];
}


- (void)webImageWithURL:(NSURL *)url
              imageSize:(CGSize)size
            placeHolder:(UIImage *)placeholder
                options:(YppWebImageOptions)options {
    [self webImageWithURL:url imageSize:size placeholder:placeholder options:options progress:nil transform:nil completion:nil];
}

- (void)webImageWithURL:(NSURL *)url
              imageSize:(CGSize)size
            placeHolder:(UIImage *)placeholder
                options:(YppWebImageOptions)options
               priority:(NSOperationQueuePriority)priority{
     [self webImageWithURL:url imageSize:size placeholder:placeholder priority:priority options:options  progress:nil transform:nil completion:nil];
}



- (void)webImageWithURL:(NSURL *)url
              imageSize:(CGSize)size
            placeholder:(UIImage *)placeholder
                options:(YppWebImageOptions)options
               progress:(YYWebImageProgressBlock)progress
              transform:(YYWebImageTransformBlock)transform
             completion:(YYWebImageCompletionBlock)completion {
    
    [self webImageWithURL:url imageSize:size placeholder:placeholder priority:NSOperationQueuePriorityNormal options:options progress:progress transform:transform completion:completion];
}

- (void)webImageWithURL:(NSURL *)url
              imageSize:(CGSize)size
            placeholder:(UIImage *)placeholder
               priority:(NSOperationQueuePriority)priority
                options:(YppWebImageOptions)options
               progress:(YYWebImageProgressBlock)progress
              transform:(YYWebImageTransformBlock)transform
             completion:(YYWebImageCompletionBlock)completion{
    if (url.absoluteString.length == 0) {
        self.image = placeholder;
        if (completion) {
            completion(nil, url, YYWebImageFromNone, YYWebImageStageFinished, nil);
        }
        return;
    }
    
    NSURL *newURL = [[YppSpecificationImage shareInstance] imageURLWithURLString:url.absoluteString cropSize:size options:options];
    options = options & ~YYWebImageOptionProgressiveBlur;
    
    
    BOOL useCustomCache = options & YppWebImageOptionUseCustomCache;
    YYWebImageManager *manager = nil;
    if (!useCustomCache) {
        manager = [YYWebImageManager sharedManager];
    } else {
        manager = [YYWebImageCustomManager sharedManager];
    }
    
    __weak typeof(self) weakSelf = self;
    [self yy_setImageWithURL:newURL placeholder:placeholder priority:priority options:(YYWebImageOptions)options manager:manager progress:progress transform:transform completion:^(UIImage * _Nullable image, NSURL * _Nonnull completionURL, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        if (error) {
            [weakSelf yy_setImageWithURL:url placeholder:placeholder options:(YYWebImageOptions)options progress:progress transform:transform completion:completion];
        }else if (completion && !error) {
            completion(image, completionURL, from, stage, error);
        }
    }];
    
}

@end
